package zhuyun.code.launcheractivity.view


import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.hardware.Camera
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import android.widget.Toast
import com.google.zxing.Result
import com.king.zxing.CaptureActivity
import kotlinx.android.synthetic.main.activity_scan_qr_code.*
import org.json.JSONObject
import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.model.Constants
import zhuyun.code.launcheractivity.utils.QRHelper

/**
 * @author: Administrator
 * @date: 2021/11/21
 */
class ScanQRCodeActivity:CaptureActivity(),View.OnClickListener {

    override fun getLayoutId(): Int {
        return R.layout.activity_scan_qr_code
    }

    override fun onCreate(icicle: Bundle?) {
        super.onCreate(icicle)
        bt_album.setOnClickListener(this)
        bt_my_qcode.setOnClickListener(this)
        iv_back.setOnClickListener(this)
        ivFlash.setOnClickListener(this)

    }

    private fun clickFlash(v:View){
        if(v.isSelected){
            offFlash()
            v.isSelected = false
        } else {
            openFlash()
            v.isSelected = true
        }
    }

    private fun offFlash(){
        val camera = cameraManager.openCamera.camera
        val parameters = camera.parameters
        parameters.flashMode = Camera.Parameters.FLASH_MODE_OFF
        camera.parameters = parameters
    }

    private fun openFlash() {
        val camera = cameraManager.openCamera.camera
        val parameters = camera.parameters
        parameters.flashMode = Camera.Parameters.FLASH_MODE_TORCH
        camera.parameters = parameters
    }

    override fun handleDecode(rawResult: Result?, barcode: Bitmap?, scaleFactor: Float) {
        val resultText = rawResult!!.text
        analyseResult(resultText)
    }

    private fun analyseResult(resultText:String){
        val obj = JSONObject(resultText)
        val type = obj.optString("QRKey")
        val id = obj.optString("QRValue")
        if(Integer.parseInt(type) == 1){
            startActivity(Intent(this,
                ProfileActivity::class.java)
                .putExtra(Constants.USER_ID,id).putExtra(Constants.SOURCE_ID,2))
            finish()
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.iv_back->{
                finish()
            }
            R.id.bt_my_qcode->{

            }
            R.id.bt_album->{
                val intent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
                intent.type = "image/*"
                startActivityForResult(intent,Constants.DEVICE_PHOTO_REQUEST)

            }
            R.id.ivFlash->{
                clickFlash(v)
            }

        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(Constants.DEVICE_PHOTO_REQUEST == requestCode){
            data?.let {
                val selectedImage = it.data!!
                val filePathColumns = arrayOf(MediaStore.Images.Media.DATA)
                val cursor = this.contentResolver.query(selectedImage,filePathColumns,null,null,null)
                cursor?.let {
                    cursor.moveToFirst()
                    val columnIndex = cursor.getColumnIndex(filePathColumns[0])
                    val imagePath = cursor.getString(columnIndex)
                    cursor.close()
                    val s = QRHelper.getResult(BitmapFactory.decodeFile(imagePath))
                    s?.let { it -> analyseResult(it) }
                    if(s == null){
                        Toast.makeText(this,"图片错误",Toast.LENGTH_SHORT).show()
                    }
                }
                if(cursor == null){
                    Toast.makeText(this,"图片路径为空",Toast.LENGTH_SHORT).show()
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }


}